package com.notime.web;

import java.util.Date;

import org.joda.time.LocalTime;

import com.notime.model.bom.Arbeitszeit;
import com.vaadin.data.Buffered.SourceException;
import com.vaadin.data.Validator;
import com.vaadin.data.Validator.InvalidValueException;
import com.vaadin.data.fieldgroup.BeanFieldGroup;
import com.vaadin.data.util.BeanItem;
import com.vaadin.data.validator.RegexpValidator;
import com.vaadin.shared.ui.datefield.Resolution;
import com.vaadin.ui.Alignment;
import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.Button.ClickListener;
import com.vaadin.ui.CustomComponent;
import com.vaadin.ui.GridLayout;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.Notification;
import com.vaadin.ui.PopupDateField;
import com.vaadin.ui.TextField;
import com.vaadin.ui.VerticalLayout;

public class MainPage extends CustomComponent {
	/**
	 * 
	 */
	private static final long serialVersionUID = -2242430002982995250L;
	private static final String MAX_WIDTH = "920px";
	private static final String FULL_SIZE = "100%";
	Arbeitszeit work = new Arbeitszeit();

	/**
	 * The constructor should first build the main layout, set the composition
	 * root and then do any custom initialization. The constructor will not be
	 * automatically regenerated by the visual editor.
	 */
	public MainPage() {
		super();
		setCompositionRoot(buildMainLayout());

		// TODO add user code here
	}

	private VerticalLayout buildMainLayout() {
		VerticalLayout mainLayout = new VerticalLayout();
		mainLayout.setImmediate(true);
		mainLayout.setWidth(MAX_WIDTH);
		mainLayout.setHeight(FULL_SIZE);
		mainLayout.setMargin(true);

		// top-level component properties
		setWidth(MAX_WIDTH);
		setHeight(FULL_SIZE);

		final HorizontalLayout mainMenuLayout = new HorizontalLayout();
		mainMenuLayout.setImmediate(true);
		mainMenuLayout.setWidth(FULL_SIZE);
		mainMenuLayout.setHeight("-1px");
		mainMenuLayout.setMargin(false);
		mainLayout.addComponent(mainMenuLayout);

		final GridLayout centerLayout = buildCenterLayout();
		mainLayout.addComponent(centerLayout);
		mainLayout.setExpandRatio(centerLayout, 1.0f);
		mainLayout.setComponentAlignment(centerLayout, new Alignment(48));

		final HorizontalLayout impressumLayout = new HorizontalLayout();
		impressumLayout.setImmediate(true);
		impressumLayout.setWidth(FULL_SIZE);
		impressumLayout.setHeight("-1px");
		impressumLayout.setMargin(false);
		mainLayout.addComponent(impressumLayout);
		mainLayout.setComponentAlignment(impressumLayout, new Alignment(24));

		return mainLayout;
	}

	private GridLayout buildCenterLayout() {
		final GridLayout centerLayout = new GridLayout();
		centerLayout.setImmediate(false);
		centerLayout.setWidth(FULL_SIZE);
		centerLayout.setHeight(FULL_SIZE);
		centerLayout.setMargin(true);
		centerLayout.setColumns(5);

		final TextField projectName = new TextField("Projektname");
		projectName.setImmediate(false);
		centerLayout.addComponent(projectName, 0, 0);

		final PopupDateField date = new PopupDateField("Datum");
		date.setImmediate(false);
		date.setWidth("145px");
		date.setResolution(Resolution.DAY);
		centerLayout.addComponent(date, 1, 0);
		date.setDateFormat("dd. MMMM yyyy");

		final TextField fromTime = new TextField("Von");
		fromTime.setImmediate(false);
		fromTime.setWidth("45px");
		centerLayout.addComponent(fromTime, 2, 0);

		final TextField toTime = new TextField("Bis");
		toTime.setImmediate(false);
		toTime.setWidth("45px");
		centerLayout.addComponent(toTime, 3, 0);

		// Validate on Focusloose
		Validator timeValidator = new RegexpValidator(
				"(0[0-9]|1[0-9]|2[0-3]):[0-5][0-9]", "Das ist keine Uhrzeit!");
		fromTime.addValidator(timeValidator);
		toTime.addValidator(timeValidator);

		// Binding
		BeanFieldGroup<Arbeitszeit> arbeitszeitBinder = new BeanFieldGroup<Arbeitszeit>(
				Arbeitszeit.class);
		arbeitszeitBinder.bindMemberFields(centerLayout);
		arbeitszeitBinder.setItemDataSource(new BeanItem<Arbeitszeit>(work));

		arbeitszeitBinder.bind(projectName, "projectName");
		arbeitszeitBinder.bind(date, "date");
		arbeitszeitBinder.bind(fromTime, "fromTime");
		arbeitszeitBinder.bind(toTime, "toTime");

		// Example Data
		work.setProjectName("Projektname 007");
		work.setDate(new Date());
		work.setFromTime(LocalTime.now().minusHours(1).toString()
				.split("\\d:[0-9]{2}\\.")[0]
				+ "0");
		work.setToTime(LocalTime.now().plusMinutes(9).toString()
				.split("\\d:[0-9]{2}\\.")[0]
				+ "0");

		final Button okButton = new Button("Ok");
		okButton.setImmediate(false);
		okButton.addClickListener(new ClickListener() {
			/**
		 * 
		 */
			private static final long serialVersionUID = -3502997225391464357L;

			@Override
			public void buttonClick(ClickEvent event) {
				try {
					projectName.commit();
					date.commit();
					fromTime.commit();
					toTime.commit();
				} catch (SourceException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (InvalidValueException e) {
					Notification.show("Illegale Werte!");
					e.printStackTrace();
					return;
				}


				Notification.show("Zeitbuchung gespeichert.\n"
						+ work.getProjectName() + " "
						+ work.getDate().toString() + " " + work.getFromTime()
						+ " " + work.getToTime());
			}
		});
		centerLayout.addComponent(okButton, 4, 0);

		return centerLayout;
	}

}
